package sort;

/**
 * 归并排序
 * https://www.cnblogs.com/of-fanruice/p/7678801.html
 * Created by JiaJia on 2019/8/15 0015.
 */
public class MergeSort {
    public static void main(String[] args) {
        int[] nums={5,1,6,3,11,7,9,2,13};
        mergeSort(nums,0,nums.length-1);
        for (int i = 0; i <nums.length ; i++) {
            System.out.println(nums[i]);
        }
    }
    private static void mergeSort(int[] nums,int low,int high){
        int middle=(low+high)/2;
        if (low<high){
            mergeSort(nums,low,middle);
            mergeSort(nums,middle+1,high);
            merge(nums,low,middle,high);
        }
    }
    private static void merge(int[] nums,int low,int mid,int high){
        int[] tmp=new int[high-low+1];
        int i=low,j=mid+1,k=0;
        while (i<=mid&&j<=high){
            if (nums[i]<nums[j]){
                tmp[k++]=nums[i++];
            }else {
                tmp[k++]=nums[j++];
            }
        }
        while (i<=mid){
            tmp[k++]=nums[i++];
        }
        while (j<=high){
            tmp[k++]=nums[j++];
        }
        for (int l = 0; l <tmp.length ; l++) {
            nums[l+low]=tmp[l];
        }


    }




}